#coding=UTF-8
#! /usr/local/bin/python
# -*- coding: UTF-8 -*-
# Filename=new_backup.py

'''
Created on 2012-10-16

@author: Dujx
'''
import sys
reload(sys) 
sys.setdefaultencoding('utf-8') 
import os
import shutil
import time
import ConfigParser
import commands
import traceback
from datetime import date,timedelta

def showMessage(str):
        '''打印字符串'''
        print  str.decode('utf-8')
def copyFile(sp,dp):
    flag=False
    if os.path.exists(os.path.dirname(sp)):
        try:
            if not os.path.exists(os.path.dirname(dp)):
	        os.makedirs(os.path.dirname(dp))            
	    if not (os.path.isfile(sp)):
                #showMessage("源文件"+sp+"不存在")
                flag=False
            shutil.copy(sp,dp)
            flag=True
        except:
            flag=False
    else:
        flag=False
    return flag
def try_int(s):
    #"Convert to integer if possible."
    try: return int(s)
    except: return s

def natsort_key(s):
    #"Used internally to get a tuple by which s is sorted."
    import re
    return map(try_int, re.findall(r'(\d+|\D+)', s))
def natcmp(a, b):
    #"Natural string comparison, case sensitive."
    return cmp(natsort_key(a), natsort_key(b))

def errlist(dirname):
    flist=[]
    if not (os.path.exists(dirname)):
        showMessage(dirname+"不存在")
        return False;
    filenames=os.listdir(dirname)
    if len(filenames) == 0:
       #showMessage("empty folder："+dirname;
       return [];
    else:
        for name in filenames:
            if (name[:5]=="error" and name[-4:]==".log" ):
                flist.append(name[:-4])
        # sort the list                
        flist.sort(natcmp)
        return flist

def logFile(efile,estr):    
    f=open(os.path.join(logErr,time.strftime('%Y-%m-%d')),'ab')
    f.write("["+time.strftime('%Y-%m-%d %X')+"]`"+estr.replace('\n', '')+'  \n')
    f.close()
    
cf=ConfigParser.ConfigParser()
cf.read("/home/ucun/htdocs/python/down/script/ucpacker/ucpacker.ini")

sp=cf.get('common','sp');
dp=cf.get('common','dp');
dn = cf.get('common','dn');
sp =sp + dn
dp =dp + dn
logsp=cf.get('common','logsp');
logdp=cf.get('common','logdp');
daydiff = int(cf.get('common','daydiff'))
scriptDir =cf.get('common','scriptDir');

#时间跨度startday-today
today = date.today();
startday = today - timedelta(daydiff-1);

#建立错误日志夹
logErr = os.path.join(scriptDir,"error")
if not (os.path.exists(logErr)):
    os.makedirs(logErr)
    
try:
    if commands.getstatusoutput('ps aux|grep '+scriptDir+'err_backup.py|grep -v grep|wc -l')==(0,'2'):
        showMessage("----[当前时间] "+time.strftime('%Y-%m-%d %X')+"----")
        # 计时
        st = time.time();

        
        # 从startday到today循环扫描
        for i in range(0,daydiff):
            checkday = startday + timedelta(i)                   
            ddir = os.path.join(logdp,str(checkday))
            #showMessage("[开始扫描] "+str(checkday)+"的error.log文件")
            

            #找到ddir最新的log       
            if not (os.path.exists(ddir)):
                    #showMessage("[扫描结束] "+str(checkday)+"当天没有包同步")
                    continue;
            # 当前下载目录文件列表
            errloglist = errlist(ddir)
            if(len(errloglist)==0):
                    #showMessage("[扫描结束] "+str(checkday)+"没有errorlog")
                    continue;
            else:
                    ssum = 0
                    fsum = 0
                    for log in errloglist:            
                            #尝试重新同步error.log的文件
                            errlog = os.path.join(ddir,log+".log")
                            if not (os.path.exists(errlog)):
                                showMessage("[扫描结束] 不存在error.log")
                                continue;
                            fr = open(errlog); 
                            #list形式读入内容
                            errorlines = fr.readlines()                            
                            fr.close()
                            linesum = len(errorlines)                            
                            if(linesum != 0):
                                    showMessage("[正在同步] "+errlog+",共"+str(linesum)+"个包")
                                    change = False
                                    faillist =[]
                                    for err in errorlines:                                       
                                        fn = err.rstrip("\n");
                                        if(fn !=""):
                                                srfile = sp+fn
                                                drfile = dp+fn                                
                                                result = copyFile(srfile,drfile)
                                                if(result == True):
                                                    ssum += 1
                                                    change = True
                                                    #showMessage("[同步成功] "+fn)                                    
                                                else:
                                                    fsum +=1
                                                    faillist.append(err)
                                                    showMessage("[同步失败] "+fn)      
                                    if(change == True):
                                        cmd = "sed -i '1,"+str(linesum)+"d' "+errlog
                                        os.system(cmd)
                                        if(len(faillist) != 0):
                                                fr = open(errlog,"ab");                                                 
                                                fr.write("".join(faillist))
                                                fr.close()                                    
                            else:
                                    #os.remove(errlog)
                                    showMessage("[扫描结束] 空的"+errlog)
                    showMessage("[本次耗时] "+str(time.time()-st)+"s,[成功数]"+str(ssum)+",[失败数]"+str(fsum))
    else:
        showMessage("已经有另一个err_backup.py在运行")
except Exception,err:
    showMessage("[发生异常] 请查看日志\n"+str(err))    
    logFile(logErr,traceback.format_exc())

    
    
    
    
    
    
    
    
    
    
    
    
